package com.jane.fragment.homepagefragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jane.adapter.homepageadapter.TitleFragmentPagerAdapter;
import com.jane.bean.JsonData;
import com.jane.bean.SeorinityTabLayoutData;
import com.jane.config.NetConfig;
import com.jane.gzzb.R;
import com.jane.utils.Utils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SeorinityFragment extends Fragment implements View.OnClickListener {
    //申明
    private static final String TAG = SeorinityFragment.class.getSimpleName();
    private View mView;
    private TabLayout mTabLayout;
    private TextView mDown;
    private ViewPager mViewPager;
    private List<SeorinityTabLayoutData.AllGameBean> mTitles;
    private List<Fragment> mFragments;
    private TitleFragmentPagerAdapter mAdapter;
    private String path;

    public SeorinityFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_seorinity, null);
        initView();
        tabLayoutToViewPager();
        initData();
        initListener();
        return mView;
    }

    private void tabLayoutToViewPager() {
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorToolBar));
        mTabLayout.setSelectedTabIndicatorHeight(3);
        mTabLayout.setTabTextColors(Color.BLACK, getResources().getColor(R.color.colorToolBar));
    }

    private void initView() {
        mDown = (TextView) mView.findViewById(R.id.fragment_seorinity_down);
        mTabLayout = (TabLayout) mView.findViewById(R.id.fragment_seorinity_tablayout);
        mViewPager = (ViewPager) mView.findViewById(R.id.fragment_seorinity_viewpager);
        mViewPager.setOffscreenPageLimit(4);
    }

    private void initData() {
        mTitles = new ArrayList<>();
        //地址
        path = NetConfig.TABLAYOUT_URL;
        String jsonString = Utils.getJsonString(path);
        if (!TextUtils.isEmpty(jsonString)) {
            parserData(jsonString);//解析数据
        } else {
            //判断是否有网
            if (!Utils.isNetwork(getActivity())) {
                return;
            }
            //网络加载数据
            networkData();
        }
    }

    private void networkData() {
        //请求数据
        x.http().get(new RequestParams(NetConfig.TABLAYOUT_URL), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
//                Log.e(TAG, "onSuccess: "+result );
                //保存一份
                JsonData jsonData = new JsonData();
                jsonData.setPath(path);
                jsonData.setJsonString(result);
                Utils.saveJsonString(jsonData);

                if (!TextUtils.isEmpty(result)) {
                    parserData(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void parserData(String result) {
        SeorinityTabLayoutData data = new Gson().fromJson(result, SeorinityTabLayoutData.class);
        if (data != null) {
            mTitles.addAll(data.getAll_game());
//                        Log.e(TAG, "onSuccess: "+mTitles.size() );
            initFragment();
        }
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.size(); i++) {
            SeprinityTabLayoutFragment fragment = new SeprinityTabLayoutFragment();
            Bundle bundle = new Bundle();
            bundle.putString("name", mTitles.get(i).getName());
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }
//        Log.e(TAG, "initFragment: "+mFragments.size() );
        //设置适配器
        mAdapter = new TitleFragmentPagerAdapter(getActivity().getSupportFragmentManager(), mTitles, mFragments);
        mViewPager.setAdapter(mAdapter);
    }

    private void initListener() {
        mDown.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_seorinity_down://下拉

                break;
        }
    }
}
